﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class PersonComponent

#Region "CRUD"
    Public Function CreatePerson(_personDetails As Person) As Person
        Return CreateNewPerson(_personDetails)
    End Function

    Public Function ReadPersonDetails(_searchPerson As String) As Person
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetPerson(_searchPerson)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function GetAllPersons() As List(Of Person)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetAllPersons()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdatePerson(_personDetail As Person) As Person
        Return UpdatePersonDetails(_personDetail)
    End Function


    Public Sub DeletePerson(_personDetails As Person)
        Dim dac As New AssetDataAccess
        Try
            dac.DeletePerson(_personDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

    End Sub
#End Region

#Region "Support Procedures"
    Private Function CreateNewPerson(personDetails As Person) As Person
        Dim dac As New AssetDataAccess()
        Try
            Return dac.CreatePerson(personDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Private Function UpdatePersonDetails(personDetail As Person) As Person
        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                Dim dac As New AssetDataAccess()
                dac.UpdatePersonDetails(personDetail)
                ts.Complete()
            Catch ex As TransactionAbortedException
                Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message)
                Throw ex
            Catch ex As ApplicationException
                Console.WriteLine("ApplicationException Message: {0}", ex.Message)
                Throw ex
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using
        Return personDetail
    End Function
#End Region

    


End Class
